package com.example.asockets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import org.json.JSONObject;

import com.google.gson.Gson;


import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;

public class network extends AsyncTask {
	
	
	public static Socket socket = null;
	public static ObjectOutputStream objectOutputStream = null;
	public static ObjectInputStream objectInputStream = null;
	
	String serverIP = "192.168.0.101";
	int port = 4444;
	
	Gson gson = new Gson();	
	
	  
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	public network(){
		
		
		
		/*
		
	    try {
	    	socket = new Socket("192.168.0.106", 4444);
           // out = new PrintWriter(socket.getOutputStream(), true);
           // in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            System.exit(1);
        }
		*/
		
	}
	

	@Override
	protected Object doInBackground(Object... params) {
		try {
			
					
			msg msgMessage = new msg("android device connected");
			String json = gson.toJson(msgMessage);
			
			
			
			
			String msgSend = "android connected";
			
			if(socket == null) {
				socket = new Socket("192.168.0.101", 4444);
				objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
				objectInputStream = new ObjectInputStream(socket.getInputStream());
				Log.v("NITESH", "connection established");
			}
			objectOutputStream.writeObject(json);
			//objectOutputStream.writeObject(msgSend);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		while (objectInputStream != null) {
			
			msg message = null;
        	//msg sendMsg = new msg("hello client");
        	
        	String str = null;
        	
        	try {
        		
        		str = (String) objectInputStream.readObject();
        		extracted(str);
        		
        		
        		Log.v("NITESH", str);
        		
        		
        	}
        	
        	catch(Exception e){
        		
        		
        	}
			
			
			
		}
		
//		try {
//			socket.close();
//			objectOutputStream.close();
//			objectInputStream.close();
//			socket = null;
//			objectInputStream = null;
//			objectOutputStream = null;
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		// TODO Auto-generated method stub
		return null;
	}

	private void extracted(String str) {
		publishProgress(str);
	}

}
